import { collectionListApi,favgoodsApi,classFavApi,classGoodApi,CancelGoodsApi,addCartOrBuy} from './api';
import { Toast } from 'antd-mobile';
import {routerRedux} from "dva/router";




export default {
    namespace: 'collectionList',
    state: {
        favotitesGoodsCommodityList:[],
        favotitesGoodsStoreList:[],
        editStatus: true,
    },
    reducers:{
        save(state, { data }) {
            return { ...state, ...data };
        },
    },
    // changeEditStatus(state) {
    //     return { ...state, editStatus: !state.editStatus };
    // },
    effects: {
        *CollectionListEFF({ obj}, { put, call }) {
            const result = yield call(collectionListApi, obj);
            //console.log(result, "数据的获取");
            if (result.code == 200) {
                yield put({ type: 'save', data: {favotitesGoodsCommodityList: result.ret.favotitesGoodsCommodityList,favotitesGoodsStoreList :result.ret.favotitesGoodsStoreList  || []} });
                Toast.hide();
            } else {

            }
        },

        *favgoodsEFF({obj},{put,call}){
            //Toast.loading("加载中...",3);
            const result = yield call(favgoodsApi,obj);
            return result;
        },
        *classFavEFF({},{put,call}){
            const result = yield call(classFavApi);
            if (result.code == 200) {
                yield put({type: 'save', data: {checks: result.ret.list}})
                // Toast.success(result.msg, 1.5);
            }else {
                // Toast.fail(result.msg, 1.5);
            }
        },
        *addCartOrBuyEFF({ obj }, { put, call, select }) {
            const result = yield call(addCartOrBuy, obj);
            if (result.code == 200) {
                if(obj.saveType==1){
                    yield put(routerRedux.push('/order'))
                }else {
                    Toast.success(result.msg, 1.5);
                }

            } else {
                // Toast.fail(result.msg, 1.5);
            }
        },
        //商品收藏-批量取消收藏
        *CancelGoodsEFF({obj},{put,call}){
            const result = yield call(CancelGoodsApi,obj);
            if (result.code == 200) {
                return result
             //   yield put({type: 'save', data: {checks: result.ret}})
             //   Toast.success(result.msg, 1.5);
            }else {
                // Toast.fail(result.msg, 1.5);
            }
        },
        *classGoodsEFF({obj},{put,call}){
            //Toast.loading("加载中...",3);
            const result = yield call(classGoodApi,obj);
            if (result.code == 200) {
                yield put({type: 'save', data: {goods: result.ret.list}})
                // Toast.success(result.msg, 1.5);
            }else {
                // Toast.fail(result.msg, 1.5);
            }
        }
    },

    subscriptions: {

    },
};
